# 文件读写

# 读文件
file1 = open("data/theZenOfPython", mode="r")
# read(n) n:n个字符
print(file1.read(10))
# readline 读取一行
print(file1.readline())
# readlines 读取所有的数据
# \n 表示 换行符
print(file1.readlines())

# 用完记得关闭
file1.close()

# 写文件
# mode 指定为 w 表示写文件(覆盖写)
# a 表示 追加写
# file2 = open("data/newFile", mode="w")
file2 = open("data/newFile", mode="a")
file2.write("abcdef\n")

# 用完记得关闭
file2.close()

f1 = open("data/theZenOfPython")
f2 = open("data/newFile2", mode="w")
for line in f1.readlines():
    f2.write(line)

# 用完记得关闭
f1.close()
f2.close()

# with open
# 不需要在手动关闭文件
with open("data/theZenOfPython") as f3:
    with open("data/newFile3", mode="w") as f4:
        for line in f3.readlines():
            f4.write(line)
